package com.accountant.travelaccountant;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class EditAccount2 extends Activity {

	ListView listViewforEditAccount2;
	EditAccount2_ListViewAdapter editAccount2_ListViewAdapter;
	ArrayList<String> memberList;
	DatabaseOperations_NonActivity databaseOperations;
	EditText input;
	ArrayList<String> attributes;
	String memberName;
	EditAccount2_GridView_Adapter editAccount2_GridView_Adapter;
	GridView gridViewForEditAccount;
	EditAccount2 editAccount2;
	TextView textViewAtTheTopOfEditcount;

	/* Size for the account */
	BigInteger accountTotalSize;
	/* The amount table for amount */
	BigInteger amountTableAmount;
	/* Average for the above amount */
	BigInteger averageOfAmount;
	/* Get all the members in an arraylist */
	ArrayList<String> memberList1;
	/* Get all the amounts paid by every member */
	ArrayList<String> memberAmounts;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		/* Linking to the xml layout */
		setContentView(R.layout.edit_account2);
		editAccount2 = this;
		/* Creating object to access its methods */
		databaseOperations = new DatabaseOperations_NonActivity(
				getApplicationContext());

		/* Initialise the controls */
		memberList = new ArrayList<String>();
		/* Initializing the gridview */
		gridViewForEditAccount = (GridView) findViewById(R.id.gridViewforeditAccount2);
		editAccount2_GridView_Adapter = new EditAccount2_GridView_Adapter(
				getApplicationContext(), this);
		gridViewForEditAccount.setAdapter(editAccount2_GridView_Adapter);
		HelperClassForActivities.addColourToGridView(gridViewForEditAccount,
				this);
		/*********************************************************************/
		/* Initializing the listview */
		listViewforEditAccount2 = (ListView) findViewById(R.id.listViewForEditAccount2);
		editAccount2_ListViewAdapter = new EditAccount2_ListViewAdapter(this,
				memberList);
		listViewforEditAccount2.setAdapter(editAccount2_ListViewAdapter);
		EditAccount2_ListViewAdapter.setTitletextOfSelectedTextview(null);

		/* Initializing the textview */
		textViewAtTheTopOfEditcount = (TextView) findViewById(R.id.textViewatTheTopForEditCount2);
		textViewAtTheTopOfEditcount.setText("Members in "
				+ SessionClass_NonActivity.sessionObject().getTitle());
		textViewAtTheTopOfEditcount.setGravity(Gravity.CENTER);
		/**************************************************************/

		/* Code to open the database */
		databaseOperations.openWriteableDatabase();

		/* Getting the list of all members and assigning to the spinner */
		memberList = databaseOperations.retrieveAll(
				DatabaseOperations_NonActivity.getMembertable(),
				new String[] { DatabaseOperations_NonActivity
						.getMembertablemember() },
				DatabaseOperations_NonActivity.getMembertabletitle() + "=\""
						+ SessionClass_NonActivity.sessionObject().getTitle()
						+ "\"", null, null, null, null);
		// adapter = new ArrayAdapter<String>(getApplicationContext(),
		// android.R.layout.simple_spinner_item, memberList);
		// spnMemberList.setAdapter(adapter);
		editAccount2_ListViewAdapter = new EditAccount2_ListViewAdapter(this,
				memberList);
		listViewforEditAccount2.setAdapter(editAccount2_ListViewAdapter);

		/* In case no members in the account */
		if (memberList.size() == 0) {
			// spnMemberList.setVisibility(View.GONE);
		}

		/* Code to close the database */
		databaseOperations.closeDatabase();
	}

	/* Dialog box for Renaming a member */
	private void showDialogBoxRenameMember(String title, String message) {

		/* Creating object to access its methods */
		databaseOperations = new DatabaseOperations_NonActivity(
				getApplicationContext());

		/* Firstly we create an object of AlertDialog.Builder */
		AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
		/* Now we initialise its contents */
		dialogBuilder.setTitle(title);
		dialogBuilder.setMessage(message);

		input = new EditText(this);
		input.setSingleLine();
		input.setText(EditAccount2_ListViewAdapter
				.getTitletextOfSelectedTextview());
		/* Fucntion to put the cursor at the last */
		Utils_NotActivity.setCursorPosition(input);
		dialogBuilder.setView(input);

		/* Event listener for changing text */
		input.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				Utils_NotActivity.validateTextLength(input);
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
			}
		});

		dialogBuilder.setPositiveButton("Rename",
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {

						/* Inserting the record */
						databaseOperations.openWriteableDatabase();

						if (databaseOperations.retrieve(
								DatabaseOperations_NonActivity.getMembertable(),
								new String[] { DatabaseOperations_NonActivity
										.getMembertablemember() },
								DatabaseOperations_NonActivity
										.getMembertabletitle()
										+ "=\""
										+ SessionClass_NonActivity
												.sessionObject().getTitle()
										+ "\" and "
										+ DatabaseOperations_NonActivity
												.getMembertablemember()
										+ "=\""
										+ input.getText().toString()
												.replaceAll("\\s+", " ").trim()
										+ "\"", null, null, null, null) != null) {
							Toast.makeText(
									getApplicationContext(),
									"Unable to rename as another member with the same name exists",
									Toast.LENGTH_SHORT).show();
							databaseOperations.closeDatabase();
							return;
						}

						/* Pop to say that the member was renamed */
						Toast.makeText(
								getApplicationContext(),
								EditAccount2_ListViewAdapter
										.getTitletextOfSelectedTextview()
										+ " was renamed to "
										+ input.getText().toString()
												.replaceAll("\\s+", " ").trim(),
								Toast.LENGTH_SHORT).show();

						/* Updating the members in the uninames table column */
						databaseOperations.updateRow(
								DatabaseOperations_NonActivity
										.getUniquenamestable(),
								DatabaseOperations_NonActivity
										.getUniquenamestablenames(),
								input.getText().toString(),
								DatabaseOperations_NonActivity
										.getUniquenamestablenames()
										+ "=\""
										+ EditAccount2_ListViewAdapter
												.getTitletextOfSelectedTextview()
										+ "\"", null);

						/* Renaming the members in the member table */
						databaseOperations.updateRow(
								DatabaseOperations_NonActivity.getMembertable(),
								DatabaseOperations_NonActivity
										.getMembertablemember(),
								input.getText().toString(),
								DatabaseOperations_NonActivity
										.getMembertabletitle()
										+ "=\""
										+ SessionClass_NonActivity
												.sessionObject().getTitle()
										+ "\" and "
										+ DatabaseOperations_NonActivity
												.getMembertablemember()
										+ "=\""
										+ EditAccount2_ListViewAdapter
												.getTitletextOfSelectedTextview()
										+ "\"", null);

						/* Renaming the members in the purpose table */
						databaseOperations.updateRow(
								DatabaseOperations_NonActivity
										.getPurposetable(),
								DatabaseOperations_NonActivity
										.getPurposetablepayingmember(),
								input.getText().toString(),
								DatabaseOperations_NonActivity
										.getPurposetabletitle()
										+ "=\""
										+ SessionClass_NonActivity
												.sessionObject().getTitle()
										+ "\" and "
										+ DatabaseOperations_NonActivity
												.getPurposetablepayingmember()
										+ "=\""
										+ EditAccount2_ListViewAdapter
												.getTitletextOfSelectedTextview()
										+ "\"", null);

						/*
						 * Getting the list of all members and assigning to the
						 * spinner
						 */
						memberList = databaseOperations.retrieveAll(
								DatabaseOperations_NonActivity.getMembertable(),
								new String[] { DatabaseOperations_NonActivity
										.getMembertablemember() },
								DatabaseOperations_NonActivity
										.getMembertabletitle()
										+ "=\""
										+ SessionClass_NonActivity
												.sessionObject().getTitle()
										+ "\"", null, null, null, null);
						editAccount2_ListViewAdapter = new EditAccount2_ListViewAdapter(
								editAccount2, memberList);
						listViewforEditAccount2
								.setAdapter(editAccount2_ListViewAdapter);
						EditAccount2_ListViewAdapter
								.setTitletextOfSelectedTextview(null);
						/* Closing the database */
						databaseOperations.closeDatabase();

					}
				});

		dialogBuilder.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						return;
					}
				});

		AlertDialog alertDialog = dialogBuilder.create();
		alertDialog.show();
	}

	/* Dialog box for Adding a member */
	private void showDialogBoxAddMember(String title, String message) {

		/* Creating object to access its methods */
		databaseOperations = new DatabaseOperations_NonActivity(
				getApplicationContext());

		/* Firstly we create an object of AlertDialog.Builder */
		AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
		/* Now we initialise its contents */
		dialogBuilder.setTitle(title);
		dialogBuilder.setMessage(message);

		input = new EditText(this);
		input.setSingleLine();
		input.setText("");
		dialogBuilder.setView(input);

		input.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				Utils_NotActivity.validateTextLength(input);
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
			}
		});

		dialogBuilder.setPositiveButton("Add",
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {

						if (input.getText().length() == 0) {
							Toast.makeText(getApplicationContext(),
									"Blank Member cannot be added",
									Toast.LENGTH_SHORT).show();
							return;
						}

						/* Inserting the record */
						databaseOperations.openWriteableDatabase();
						/* Preparing the record column by column */
						attributes = new ArrayList<String>();
						attributes.add(SessionClass_NonActivity.sessionObject()
								.getTitle());
						attributes.add(input.getText().toString());

						/* Adding the member to the member table */
						if (databaseOperations.insertIntoTable(
								DatabaseOperations_NonActivity.getMembertable(),
								Utils_NotActivity.memberTableColumns(),
								attributes) != -1) {

							/* Inserting into the unique table names column */
							databaseOperations.insertIntoTable(
									DatabaseOperations_NonActivity
											.getUniquenamestable(),
									Utils_NotActivity.uniqueNamesTableColumns(),
									new ArrayList<String>(Arrays.asList(input
											.getText().toString())));

							Toast.makeText(
									getApplicationContext(),
									input.getText().toString()
											.replaceAll("\\s+", " ").trim()
											+ " has been added as a member to the account "
											+ SessionClass_NonActivity
													.sessionObject().getTitle(),
									Toast.LENGTH_SHORT).show();

							/*
							 * Getting the list of all members and assigning to
							 * the spinner
							 */
							memberList = databaseOperations.retrieveAll(
									DatabaseOperations_NonActivity
											.getMembertable(),
									new String[] { DatabaseOperations_NonActivity
											.getMembertablemember() },
									DatabaseOperations_NonActivity
											.getMembertabletitle()
											+ "=\""
											+ SessionClass_NonActivity
													.sessionObject().getTitle()
											+ "\"", null, null, null, null);

							/* Update the number of members in Amount Table */
							databaseOperations.updateRow(
									DatabaseOperations_NonActivity
											.getAmounttable(),
									DatabaseOperations_NonActivity
											.getAmounttabletotalmembers(),
									String.valueOf(memberList.size()),
									DatabaseOperations_NonActivity
											.getAmounttabletitle()
											+ "=\""
											+ SessionClass_NonActivity
													.sessionObject().getTitle()
											+ "\"", null);

							editAccount2_ListViewAdapter = new EditAccount2_ListViewAdapter(
									editAccount2, memberList);
							listViewforEditAccount2
									.setAdapter(editAccount2_ListViewAdapter);
							/*
							 * Updating the member table by calling the function
							 * of purpose class
							 */
							updateToGetAndToGiveColumns();

							attributes = new ArrayList<String>();
							databaseOperations.closeDatabase();
							return;
						} else
							Toast.makeText(getApplicationContext(),
									"Member is already added",
									Toast.LENGTH_SHORT).show();
						attributes = new ArrayList<String>();
						databaseOperations.closeDatabase();
						return;
					}
				});

		dialogBuilder.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						return;
					}
				});

		AlertDialog alertDialog = dialogBuilder.create();
		alertDialog.show();
	}

	/* Dialog box for Deleting a member */
	private void showDialogBoxDeleteMember(String title, String message) {

		/* Creating object to access its methods */
		databaseOperations = new DatabaseOperations_NonActivity(
				getApplicationContext());

		/* Firstly we create an object of AlertDialog.Builder */
		AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
		/* Now we initialise its contents */
		dialogBuilder.setTitle(title);
		dialogBuilder.setMessage(message);
		dialogBuilder.setPositiveButton("Yes",
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						/* Code to open the database */
						databaseOperations.openWriteableDatabase();

						/*
						 * Getting the member amount from the member table for
						 * that record you are trying to delete
						 */
						BigInteger memberAmount = new BigInteger(
								databaseOperations.retrieve(
										DatabaseOperations_NonActivity
												.getMembertable(),
										new String[] { DatabaseOperations_NonActivity
												.getMembertableamount() },
										DatabaseOperations_NonActivity
												.getMembertabletitle()
												+ "=\""
												+ SessionClass_NonActivity
														.sessionObject()
														.getTitle()
												+ "\" and "
												+ DatabaseOperations_NonActivity
														.getMembertablemember()
												+ "=\""
												+ EditAccount2_ListViewAdapter
														.getTitletextOfSelectedTextview()
												+ "\"", null, null, null, null));

						/* Getting the total amount for that title */
						BigInteger totalAmount = new BigInteger(
								databaseOperations.retrieve(
										DatabaseOperations_NonActivity
												.getAmounttable(),
										new String[] { DatabaseOperations_NonActivity
												.getAmounttableamount() },
										DatabaseOperations_NonActivity
												.getAmounttabletitle()
												+ "=\""
												+ SessionClass_NonActivity
														.sessionObject()
														.getTitle() + "\"",
										null, null, null, null));

						/* Deleting the member from the member table */
						if (databaseOperations.deleteRow(
								DatabaseOperations_NonActivity.getMembertable(),
								DatabaseOperations_NonActivity
										.getMembertabletitle()
										+ "=\""
										+ SessionClass_NonActivity
												.sessionObject().getTitle()
										+ "\" and "
										+ DatabaseOperations_NonActivity
												.getMembertablemember()
										+ "=\""
										+ EditAccount2_ListViewAdapter
												.getTitletextOfSelectedTextview()
										+ "\"", null) != 0) {

							/*
							 * Deleting from uniquetablenames
							 */
							databaseOperations.deleteRow(
									DatabaseOperations_NonActivity
											.getUniquenamestable(),
									DatabaseOperations_NonActivity
											.getUniquenamestablenames()
											+ "=\""
											+ EditAccount2_ListViewAdapter
													.getTitletextOfSelectedTextview()
											+ "\"", null);

							/*
							 * Deleting all particular records in the Purpose
							 * table
							 */
							databaseOperations.deleteRow(
									DatabaseOperations_NonActivity
											.getPurposetable(),
									DatabaseOperations_NonActivity
											.getPurposetabletitle()
											+ "=\""
											+ SessionClass_NonActivity
													.sessionObject().getTitle()
											+ "\" and "
											+ DatabaseOperations_NonActivity
													.getPurposetablepayingmember()
											+ "=\""
											+ EditAccount2_ListViewAdapter
													.getTitletextOfSelectedTextview()
											+ "\"", null);

							/*
							 * Updating the amount in the amount table because
							 * he may have some given some amount
							 */
							databaseOperations.updateRow(
									DatabaseOperations_NonActivity
											.getAmounttable(),
									DatabaseOperations_NonActivity
											.getAmounttableamount(),
									String.valueOf(totalAmount.intValue()
											- memberAmount.intValue()),
									DatabaseOperations_NonActivity
											.getAmounttabletitle()
											+ "=\""
											+ SessionClass_NonActivity
													.sessionObject().getTitle()
											+ "\"", null);

							/*
							 * Getting the list of all members and assigning to
							 * the spinner because after deleting the member we
							 * need to update
							 */
							memberList = databaseOperations.retrieveAll(
									DatabaseOperations_NonActivity
											.getMembertable(),
									new String[] { DatabaseOperations_NonActivity
											.getMembertablemember() },
									DatabaseOperations_NonActivity
											.getMembertabletitle()
											+ "=\""
											+ SessionClass_NonActivity
													.sessionObject().getTitle()
											+ "\"", null, null, null, null);
							// adapter = new ArrayAdapter<String>(
							// getApplicationContext(),
							// android.R.layout.simple_spinner_item,
							// memberList);
							// spnMemberList.setAdapter(adapter);
							editAccount2_ListViewAdapter = new EditAccount2_ListViewAdapter(
									editAccount2, memberList);
							listViewforEditAccount2
									.setAdapter(editAccount2_ListViewAdapter);

							/* Update the number of members in Amount Table */
							databaseOperations.updateRow(
									DatabaseOperations_NonActivity
											.getAmounttable(),
									DatabaseOperations_NonActivity
											.getAmounttabletotalmembers(),
									String.valueOf(memberList.size()),
									DatabaseOperations_NonActivity
											.getAmounttabletitle()
											+ "=\""
											+ SessionClass_NonActivity
													.sessionObject().getTitle()
											+ "\"", null);

							if (memberList.size() == 0) {
								// spnMemberList.setVisibility(View.GONE);
							}

							/*
							 * Updating the member table by calling the function
							 * of purpose class
							 */
							updateToGetAndToGiveColumns();

							/* Code to close the database */
							databaseOperations.closeDatabase();
							EditAccount2_ListViewAdapter
									.setTitletextOfSelectedTextview(null);
							return;
						} else {
							Toast.makeText(getApplicationContext(),
									"Unable to delete", Toast.LENGTH_SHORT)
									.show();
							databaseOperations.closeDatabase();
							return;
						}
					}
				});

		dialogBuilder.setNegativeButton("No",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						return;
					}
				});

		AlertDialog alertDialog = dialogBuilder.create();
		alertDialog.show();
	}

	public void updateToGetAndToGiveColumns() {
		/* Retrieve the size of the Amount Table */
		accountTotalSize = new BigInteger(databaseOperations.retrieve(
				DatabaseOperations_NonActivity.getAmounttable(),
				new String[] { DatabaseOperations_NonActivity
						.getAmounttabletotalmembers() },
				DatabaseOperations_NonActivity.getAmounttabletitle() + "=\""
						+ SessionClass_NonActivity.sessionObject().getTitle()
						+ "\"", null, null, null, null));

		/*
		 * Incase all the members have been deleted then this function should
		 * not be allowed to proceed further
		 */
		if (accountTotalSize.intValue() == 0)
			return;
		/* Retrieve the amount table amount */
		amountTableAmount = new BigInteger(databaseOperations.retrieve(
				DatabaseOperations_NonActivity.getAmounttable(),
				new String[] { DatabaseOperations_NonActivity
						.getAmounttableamount() },
				DatabaseOperations_NonActivity.getAmounttabletitle() + "=\""
						+ SessionClass_NonActivity.sessionObject().getTitle()
						+ "\"", null, null, null, null));
		/* Calculating the average */
		averageOfAmount = amountTableAmount.divide(accountTotalSize);

		/* Retrieving all the members of the particular account */
		memberList1 = databaseOperations.retrieveAll(
				DatabaseOperations_NonActivity.getMembertable(),
				new String[] { DatabaseOperations_NonActivity
						.getMembertablemember() },
				DatabaseOperations_NonActivity.getMembertabletitle() + "=\""
						+ SessionClass_NonActivity.sessionObject().getTitle()
						+ "\"", null, null, null, null);

		/* Retrieving all the amounts of the particular accounts */
		memberAmounts = databaseOperations.retrieveAll(
				DatabaseOperations_NonActivity.getMembertable(),
				new String[] { DatabaseOperations_NonActivity
						.getMembertableamount() },
				DatabaseOperations_NonActivity.getMembertabletitle() + "=\""
						+ SessionClass_NonActivity.sessionObject().getTitle()
						+ "\"", null, null, null, null);

		for (int i = 0; i < accountTotalSize.intValue(); i++) {
			/*
			 * Suppose a person has given more then he has to receive from his
			 * friends
			 */
			if ((new BigInteger(memberAmounts.get(i))).intValue() >= averageOfAmount
					.intValue()) {
				/* Updating the to get column */
				databaseOperations.updateRow(
						DatabaseOperations_NonActivity.getMembertable(),
						DatabaseOperations_NonActivity.getMembertabletoget(),
						String.valueOf((new BigInteger(memberAmounts.get(i)))
								.intValue() - averageOfAmount.intValue()),
						DatabaseOperations_NonActivity.getMembertabletitle()
								+ "="
								+ "\""
								+ SessionClass_NonActivity.sessionObject()
										.getTitle()
								+ "\" and "
								+ DatabaseOperations_NonActivity
										.getMembertablemember() + "=" + "\""
								+ memberList1.get(i) + "\"", null);

				/* Making the to give columns */
				databaseOperations.updateRow(
						DatabaseOperations_NonActivity.getMembertable(),
						DatabaseOperations_NonActivity.getMembertabletogive(),
						String.valueOf(0),
						DatabaseOperations_NonActivity.getMembertabletitle()
								+ "="
								+ "\""
								+ SessionClass_NonActivity.sessionObject()
										.getTitle()
								+ "\" and "
								+ DatabaseOperations_NonActivity
										.getMembertablemember() + "=" + "\""
								+ memberList1.get(i) + "\"", null);
			}
			/*
			 * Suppose a person has given less then he has to give to his
			 * friends
			 */
			else if ((new BigInteger(memberAmounts.get(i))).intValue() < averageOfAmount
					.intValue()) {
				/* Updating the to give column */
				databaseOperations.updateRow(
						DatabaseOperations_NonActivity.getMembertable(),
						DatabaseOperations_NonActivity.getMembertabletogive(),
						String.valueOf(averageOfAmount.intValue()
								- (new BigInteger(memberAmounts.get(i)))
										.intValue()),
						DatabaseOperations_NonActivity.getMembertabletitle()
								+ "="
								+ "\""
								+ SessionClass_NonActivity.sessionObject()
										.getTitle()
								+ "\" and "
								+ DatabaseOperations_NonActivity
										.getMembertablemember() + "=" + "\""
								+ memberList1.get(i) + "\"", null);

				databaseOperations.updateRow(
						DatabaseOperations_NonActivity.getMembertable(),
						DatabaseOperations_NonActivity.getMembertabletoget(),
						String.valueOf(0),
						DatabaseOperations_NonActivity.getMembertabletitle()
								+ "="
								+ "\""
								+ SessionClass_NonActivity.sessionObject()
										.getTitle()
								+ "\" and "
								+ DatabaseOperations_NonActivity
										.getMembertablemember() + "=" + "\""
								+ memberList1.get(i) + "\"", null);
			}
		}
	}

	public void addMember() {
		/*
		 * Check for if the database does not exist go directly go to front
		 * screen
		 */
		if (Utils_NotActivity.databaseDoesNotExist(getApplicationContext())) {
			Intent intent1 = new Intent(EditAccount2.this, FrontScreen.class);
			startActivity(intent1);
			return;
		}
		showDialogBoxAddMember("Add a member", "Enter the name");
	}

	public void editPurpose() {
		/* Check for the account being selected or not */
		if (checkIfAnyAccountHasBeenSelectedOrNot()) {
			return;
		}
		/*
		 * Check for if the database does not exist go directly go to front
		 * screen
		 */
		if (Utils_NotActivity.databaseDoesNotExist(getApplicationContext())) {
			Intent intent1 = new Intent(EditAccount2.this, FrontScreen.class);
			startActivity(intent1);
			return;
		}
		Intent intent = new Intent(EditAccount2.this, EditPurpose2.class);
		Bundle bundle = new Bundle();
		bundle.putString("member",
				EditAccount2_ListViewAdapter.getTitletextOfSelectedTextview());
		intent.putExtras(bundle);
		startActivity(intent);
	}

	public void rename() {
		/* Check for the account being selected or not */
		if (checkIfAnyAccountHasBeenSelectedOrNot()) {
			return;
		}
		/*
		 * Check for if the database does not exist go directly go to front
		 * screen
		 */
		if (Utils_NotActivity.databaseDoesNotExist(getApplicationContext())) {
			Intent intent1 = new Intent(EditAccount2.this, FrontScreen.class);
			startActivity(intent1);
			return;
		}
		showDialogBoxRenameMember(
				"Rename "
						+ EditAccount2_ListViewAdapter
								.getTitletextOfSelectedTextview(),
				"Enter the name");
	}

	public void deleteMember() {
		/* Check for the account being selected or not */
		if (checkIfAnyAccountHasBeenSelectedOrNot()) {
			return;
		}
		/*
		 * Check for if the database does not exist go directly go to front
		 * screen
		 */
		if (Utils_NotActivity.databaseDoesNotExist(getApplicationContext())) {
			Intent intent1 = new Intent(EditAccount2.this, FrontScreen.class);
			startActivity(intent1);
			return;
		}

		showDialogBoxDeleteMember("Delete Member", "Do you want to delete "
				+ EditAccount2_ListViewAdapter.getTitletextOfSelectedTextview());
	}

	/* A function to check whether the account was selected or not */
	private boolean checkIfAnyAccountHasBeenSelectedOrNot() {
		if (EditAccount2_ListViewAdapter.getTitletextOfSelectedTextview() == null) {
			Toast.makeText(getApplicationContext(), "No Member Selected",
					Toast.LENGTH_SHORT).show();
			return true;
		}
		return false;
	}

}
